package com.yueya.api.stay;

import net.sf.json.JSONObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.aop.Duang;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.yueya.basic.dict.model.Dict;
import com.yueya.basic.news.content.model.Content;
import com.yueya.basic.news.content.model.ContentZan;
import com.yueya.business.bed.model.Bed;
import com.yueya.business.senior.model.SeniorBase;
import com.yueya.business.senior.model.SeniorGuardian;
import com.yueya.business.stay.model.Stay;
import com.yueya.business.stay.model.StayLeave;
import com.yueya.common.constant.CodeConst;
import com.yueya.common.model.Resp;
import com.yueya.common.service.BaseService;
import com.yueya.common.util.DateUtil;
import com.yueya.common.util.UUIDGenerator;

public class IStayService extends BaseService {

	/**
	 * 获取列表
	 * 
	 * @param jo
	 * @return
	 */
	public Resp getPageList(JSONObject jo) {
		if (containsKey(jo, "pgn", "pgs")) {

			if (jo.containsKey("pgn")) {
				pageNumber = jo.getInt("pgn");
			}
			if (jo.containsKey("pgs")) {
				pageSize = jo.getInt("pgs");
			}
			Page page = Stay.dao.paginate(pageNumber, pageSize, jo);
			if (page != null) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setData(page);
				resp.setMsg("some page data");
			} else {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("no menu data");
			}
		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("参数错误");
		}
		return resp;
	}

	// 获取明细
	public Resp getDetail(JSONObject jo) {
		if (containsKey(jo, "stayid")) {
			String id = jo.getString("stayid");
			Stay r = Stay.dao.getDetailById(id);
			if (r != null) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setData(r);
			} else {
				resp.setCode(CodeConst.SUCCESS);
			}

		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("参数错误");
			
		}
		return resp;
	}


	/**
	 * 保存
	 * 
	 * @param jo
	 * @return
	 */
	@Before(Tx.class)
	public Resp save(JSONObject jo) {
		if (containsKey(jo, "seniorid","bunkid")) {
			String seniorid = jo.getString("seniorid");
			String feestatus = jo.getString("fee_status");
			String bedid = jo.getString("bed_id");
			String staystartdate = jo.getString("stay_start_date");
			String stayenddate = jo.getString("stay_end_date");
			String registrantdate = jo.getString("registrant_date");
			String status = jo.getString("status");
			String userid = jo.getString("userid");
			String stayid= UUIDGenerator.getUUID();
			Stay stay = Duang.duang(Stay.class);
			stay.set("stay_id", stayid);
			stay.set("senior_id", seniorid);
			stay.set("fee_status", feestatus);
			stay.set("bed_id", bedid);
			stay.set("stay_start_date", staystartdate);
			stay.set("stay_end_date", stayenddate);
			stay.set("registrant_user_id", userid);
			stay.set("registrant_date", registrantdate);
			stay.set("status", status);
			boolean  bl= Stay.dao.save(stay);
			if (bl) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("保存成功");
			} else {
				resp.setCode(CodeConst.ERROR);
				resp.setMsg("保存失败");
			}

		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}

		return resp;
	}
	
	
	/**
	 * 修改
	 * 
	 * @param jo
	 * @return
	 */
	@Before(Tx.class)
	public Resp update(JSONObject jo) {
		if (containsKey(jo,"stayid", "seniorid","bunkid")) {
			String seniorid = jo.getString("seniorid");
			String feestatus = jo.getString("fee_status");
			String bedid = jo.getString("bed_id");
			String staystartdate = jo.getString("stay_start_date");
			String stayenddate = jo.getString("stay_end_date");
			String registrantdate = jo.getString("registrant_date");
			String status = jo.getString("status");
			String userid = jo.getString("userid");
			String stayid = jo.getString("stayid");
			Stay stay = Duang.duang(Stay.class);
			stay.set("stay_id", stayid);
			stay.set("senior_id", seniorid);
			stay.set("fee_status", feestatus);
			stay.set("bed_id", bedid);
			stay.set("stay_start_date", staystartdate);
			stay.set("stay_end_date", stayenddate);
			stay.set("registrant_user_id", userid);
			stay.set("registrant_date", registrantdate);
			stay.set("status", status);
			boolean  bl= Stay.dao.update(stay);
			if (bl) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("保存成功");
			} else {
				resp.setCode(CodeConst.ERROR);
				resp.setMsg("保存失败");
			}
		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}

		return resp;
	}
	
	
	/**
	 * 删除
	 * 
	 * @param jo
	 * @return
	 */
	@Before(Tx.class)
	public Resp delete(JSONObject jo) {
		if (containsKey(jo, "stayid")) {
			String stayid= jo.getString("stayid");;
			Stay r = Stay.dao.getDetailById(stayid);
			r.set("del_flag", "1");
			boolean b1= Stay.dao.update(r);
			if (b1) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("删除成功");
			} else {
				resp.setCode(CodeConst.ERROR);
				resp.setMsg("删除失败");
			}

		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}

		return resp;
	}
	
	
	/**
	 * 退住
	 * 
	 * @param jo
	 * @return
	 */
	@Before(Tx.class)
	public Resp stayLeave(JSONObject jo) {
		if (containsKey(jo, "stayid")) {
			String stayid= jo.getString("stayid");
			Stay r = Stay.dao.getDetailById(stayid);
			r.set("status", "2");
			boolean b1= Stay.dao.update(r);
			StayLeave sl = Duang.duang(StayLeave.class);
			sl.set("stay_leave_id", UUIDGenerator.getUUID());
			sl.set("stay_id", stayid);
			sl.set("leave_date", jo.getString("leavedate"));
			sl.set("leave_note", jo.getString("leavenote"));
			sl.set("registrant_user_id", jo.getString("userid"));
			boolean b2= StayLeave.dao.save(sl);
			if (b1&&b2) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("操作成功");
			} else {
				resp.setCode(CodeConst.ERROR);
				resp.setMsg("操作失败");
			}
		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}

		return resp;
	
	}

}
